package com.ganyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganyu.domain.ResponseResult;
import com.ganyu.domain.dto.AdminTagDto;
import com.ganyu.domain.vo.AdminTagVo;
import com.ganyu.domain.vo.PageVo;
import com.ganyu.domain.vo.TagVo;
import com.ganyu.mapper.TagMapper;
import com.ganyu.domain.entity.Tag;
import com.ganyu.service.TagService;
import com.ganyu.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static sun.security.krb5.Confounder.intValue;

/**
 * 标签(Tag)表服务实现类
 *
 * @author chenxi
 * @since 2022-09-27 23:32:25
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;
    /**
     * 查询标签
     */
    @Override
    public ResponseResult getTagList(Integer pageNum, Integer pageSize, AdminTagDto adminTagDto) {
        LambdaQueryWrapper<Tag> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.hasText(adminTagDto.getName()),Tag::getName,adminTagDto.getName());
        query.eq(StringUtils.hasText(adminTagDto.getRemark()),Tag::getRemark,adminTagDto.getRemark());

        //进行分页
        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        page(page,query);
        //封装数据
        PageVo<Tag> adminTagVo = new PageVo<>();
        adminTagVo.setRows(page.getRecords());
        adminTagVo.setTotal((int)page.getTotal());
        return ResponseResult.okResult(adminTagVo);
    }

    //添加标签
    @Override
    public ResponseResult insertTag(Tag tag) {
        tagMapper.insert(tag);
        return ResponseResult.okResult();
    }

    //删除标签
    @Override
    public ResponseResult deleteTag(Long[] ids) {
        tagMapper.deleteTagById(ids);
        return ResponseResult.okResult();
    }

    //根据id查询标签
    @Override
    public ResponseResult<TagVo> getTagById(Long id) {
        Tag tag = tagMapper.selectById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    //修改标签
    @Override
    public ResponseResult updateTag(Tag tag) {
        tagMapper.updateById(tag);
        return ResponseResult.okResult();
    }

    //查询所有标签
    @Override
    public ResponseResult getAllTag() {
        List<Tag> tagList = tagMapper.getTagList();
        List<AdminTagVo> adminTagVos = BeanCopyUtils.copyBeanList(tagList, AdminTagVo.class);
        return ResponseResult.okResult(adminTagVos);
    }
}

